#!/usr/bin/perl 
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Cookies;  
use HTTP::Headers;
use Encode;
use strict;
#---------glob variable-------
my ($password, $username);
open CONFIG,"<$ENV{'HOME'}/.newsmth-fetion.config";
foreach(<CONFIG>){
    chomp;
    $username=$1 if /username=(.*)/;
    $password=$1 if /password=(.*)/;
}


my $browser=LWP::UserAgent->new(
    timeout =>10,
    cookie_jar=> HTTP::Cookies->new( 
	file=> "$ENV{'HOME'}/.newsmth_cookie.dat",
	autosave=>1,
	ignore_discard=>1,
    )
);
my %ftype_subfix=(
    digest=>"&ftype=1",
    reserve=>"&ftype=3",
    unitopic=>"&ftype=6",
);
#----------main action----------
#&login_newsmth;
$_=&get_board("test");
while(/<body>您还没有登录，或者你发呆时间过长被服务器清除。 请重新/){
    &login_newsmth;
    $_=&get_board("test");
}
my $ref_parser=&get_pages_of_article_on_board("seconddigi",1);
foreach(@$ref_parser){
    next if $_->{"paid"}!=$_->{"aid"};
    &article_parse($_);
    print $_->{"title"},"@",$_->{"time"}," by ",$_->{"author"},"\n";
    print $_->{"content"};
    print "\n\n";
}
#print &logout_newsmth;
#--------define function-------
sub article_parse{
    #@parm   $ref_to_article_ref ref to a article_info 
    my $ref_to_article_info=shift;
    my $request=HTTP::Request->new(
	"GET",$ref_to_article_info->{"url"},
    );
    my $response=$browser->request($request);
    my $content=encode("utf-8",decode("gbk",$response->content));;
    $content=$1 if $content=~/^prints(.*)$/m;
    # print $content."\n";
    my $time=$1 if $content=~/\((\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4})\)/;
    $content=$1 if $content=~/站内\\n\\n(.*)\\n\\n/;
    $content=~s/\\r//g;
    $content=~s/(来源:·水木社区.*)//g;
    $content=~s/\[\d*m//g;
    $content=~s/\\n*(--)?//g;
    $content=~s/[\s\t]{2,}/  /g;
    $ref_to_article_info->{"time"}= $time;
    $ref_to_article_info->{"content"}= $content;
}
sub get_pages_of_article_on_board{
    #@parm   $boardname which board you need
    #	     $num_of_page ,how many page you want to get default num is 1
    #@return  \@article_info
    my @article_info;
    my ($boardname,$num_of_page)=@_;
    $num_of_page=1 unless defined $num_of_page;
    my $page_num,$ref_parser;
    while($num_of_page--){
	($page_num,$ref_parser)=&board_parse(&get_board($boardname," ",$page_num));
	$page_num--;
	push @article_info,@$ref_parser;
    }
    @article_info=sort {$$a{paid}<=>$$b{paid}
	    or
	$$a{aid}<=>$$b{aid}
    } @article_info;
    my @uniq_article_info;
    my $last_aid;
    foreach(@article_info){
	my $this_aid=$$_{aid};
	push (@uniq_article_info,$_) unless $this_aid==$last_aid; 
	$last_aid=$this_aid;
    }
    return \@uniq_article_info;
}
sub board_parse{
    #@parm $source_of_board the source code of board_page
    #  
    #@return	$page_num the num of current  page
    #		\@article_info  contain refe to annoymous hash
    #			    ->title
    #			    ->author
    #			    ->time
    #			    ->url: url to  artile
    #			    ->aid
    #			    ->paid
    $_=$_[0];
    /<script type="text\/javascript">(.*?)<\/script>/s;
    $_=$1;
    @_=split /\n/;
    pop @_ for(1..3),shift @_;#remove  dummy lines
    (shift @_)=~/docWriter\((.*?)\)/;
    my $page_num=(split /,/,$1)[5];;
    my $bid=(split /,/,$1)[1];;
    my @article_info;
    foreach(@_){
	/^c\.o\((.*?)\);$/;
	my @attr=split /,/,$1;
	next if $attr[3]=~/'[Dd] '/;
	$attr[5]=~s/^'|'$//g;
	$attr[2]=~s/^'|'$//g;
	push @article_info,{
	    title=>$attr[5],
	    author=>$attr[2],    
	    url=>"http://www.newsmth.net/bbscon.php?bid=${bid}&id=${attr[0]}",
	    aid=>$attr[0],	    
	    paid=>$attr[1],
	};
    }
    return ($page_num,\@article_info);
}
sub login_newsmth{
    my $response=$browser->post('http://www.newsmth.net/bbslogin2.php',
	[
	"id"=> $username,
	"passwd" =>$password,
	"x" => "25",
	"y" => "6",
	],
    );   
    return 1 unless $response->is_success;
    return encode("utf-8",decode("gbk",$response->content));
}
sub get_board{
    #@parm $boardname is the name of board on newsmth, such as "joke","basketball" and so on
    #@parm $ftype is the type of readmode, such as "digest","unitopic"
    #@parm $page is the page want to get 
    my ($boardname,$ftype,$page)=@_;
    $page="&page=$page" if defined $page;
    $ftype=$ftype_subfix{$ftype};
    my $request=HTTP::Request->new(
	"GET","http://www.newsmth.net/bbsdoc.php?board=${boardname}${ftype}${page}"
    );
    #print "http://www.newsmth.net/bbsdoc.php?board=${boardname}${ftype}${page}\n";
    my $response=$browser->request($request);
    return 1 unless $response->is_success;
    return encode("utf-8",decode("gbk",$response->content));;
}
sub logout_newsmth{
    my $request=HTTP::Request->new(
	"GET","http://www.newsmth.net/bbslogout.php",
    );
    my  $response=$browser->request($request);
    return 1 unless $response->is_success;
    return encode("utf-8",decode("gb2312",$response->content));
}
